<template>
  <el-table :data="cartList" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="80px"></el-table-column>
    <el-table-column label="商品信息" width="500px">
      <template #="{ row, $index }">
        <div class="goodsInfo">
          <RouterLink to="/"
            ><img :src="row.picture" alt="" class="img"
          /></RouterLink>
          <div>
            <p class="name ellipsis">{{ row.name }}</p>
          </div>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="单价">
      <template #="{ row, $index }">
        <p class="name ellipsis">{{ row.price }}</p>
      </template>
    </el-table-column>
    <el-table-column label="数量" width="200px" align="center">
      <template #="{ row, $index }">
        <el-input-number v-model="row.count" :min="1" />
      </template>
    </el-table-column>
    <el-table-column label="小计" width="100px" align="center">
      <template #="{ row, $index }">
        <div class="goodsPrice">
          &yen;{{ (row.count * row.price).toFixed(2) }}
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作" align="center">
      <template #="{ row, $index }">
        <el-popconfirm
          width="300px"
          :title="`你确定要删除吗${row.name}?`"
          confirm-button-text="确认"
          cancel-button-text="取消"
          @confirm="delCart(row.skuId)"
        >
          <template #reference>
            <el-button type="primary">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 操作栏 -->
  <div class="action">
    <div class="batch">
      共 10 件商品，已选择 2 件，商品合计：
      <span class="totalPrice">¥ 200.00 </span>
    </div>
    <div class="balance">
      <el-button type="primary">下单结算</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineOptions, ref } from "vue";
//引入购物车仓库
import { useCartStore } from "@/stores/modules/shoppingCartStore";
//保持响应式数据不丢失
import { storeToRefs } from "pinia";
defineOptions({
  name: "ShopCart",
});
//应用购物车仓库
const cartStore = useCartStore();
//解构购物车仓库中的数据
const { cartList } = storeToRefs(cartStore);
//解构购物车仓库中的方法
const { delCart } = cartStore;

//TODO ?.
const handleSelectionChange = (selection) => {
  console.log(selection);
  // cartStore.singleCheck(selection);
};

</script>

<style scoped lang="scss">
.goodsPrice {
  color: red;
}
.goodsInfo {
  display: flex;
  align-items: center;
  a {
    display: block;

    .img {
      width: 100px;
      height: 100px;
    }
  }
  .name {
    margin-left: 20px;
  }
}

.action {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 60px 20px 15px;
  background-color: #fff;
  margin-top: 20px;
  .totalPrice {
    color: red;
    font-weight: 600;
    font-size: 20px;
  }
}
</style>
